//
//  VRHomeCateView.swift
//  VoiceRoom
//
//  Created by Rui on 2024/3/16.
//

import UIKit

class VRHomeHotCateView: UIView {

    private let collectionView = UICollectionView(frame: .zero, collectionViewLayout: UICollectionViewFlowLayout())
    private var dataList:[VRTagItemModel] = {
       let arr = ["All","Male","Female","Voice"]
        var data = [VRTagItemModel]()
        for str in arr {
            var isSel = false
            if str == "All"{
                isSel = true
            }
            let model = VRTagItemModel(title: str, isSel: isSel)
            data.append(model)
        }
        return data
    }()
    
    var hotCateBlock: ((_ cateType: Int) -> Void)?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        subviews{
            collectionView
        }
        
        collectionView.leading(0).trailing(0).top(7).height(30)
        
        let layout = UICollectionViewLeftAlignedLayout()
        layout.scrollDirection = .vertical
        layout.minimumLineSpacing = 10
        layout.minimumInteritemSpacing = 10
        layout.sectionInset = UIEdgeInsets(top: 0, left: 10, bottom: 0, right: 10)
        collectionView.style { v in
            v.collectionViewLayout = layout
            v.backgroundColor = .clear
            v.register(VRTagCollectionItem.self, forCellWithReuseIdentifier: VRTagCollectionItem.description())
            v.showsVerticalScrollIndicator = false
            v.delegate = self
            v.dataSource = self
        }
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

extension VRHomeHotCateView: UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return dataList.count
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        
        guard let item = collectionView.dequeueReusableCell(withReuseIdentifier: VRTagCollectionItem.description(), for: indexPath) as? VRTagCollectionItem else { return UICollectionViewCell() }
        item.model = dataList[indexPath.row]
        return item
    }
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {

        var models = [VRTagItemModel]()
        dataList.forEach { model in
            var itemModel = model
            itemModel.isSel = false
            models.append(itemModel)
        }
        models[indexPath.row].isSel = true
        dataList = models
        collectionView.reloadData()
        
        let cateType = indexPath.row + 1
        hotCateBlock?(cateType)
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        let model = dataList[indexPath.row]
//        var w = model.title.caculateWidth(CGSize(width: 85, height: 30), 14.font_medium!)
        var w = (model.title as NSString).size(withAttributes: [NSAttributedString.Key.font: 14.font_medium!]).width
        w = w + 20.0
        return CGSize(width: w, height: 30)
    }
}


class VRHomeDiscoverCateView: UIView {

    private lazy var titleLabel = c_make_ex(UILabel.self)
        .color(.white).font(16.font_semiBold)
        .text("Language").end()
    private lazy var popularLabel = c_make_ex(UILabel.self)
        .color(.white).font(16.font_semiBold)
        .text("Popular").end()
    private let collectionView = UICollectionView(frame: .zero, collectionViewLayout: UICollectionViewFlowLayout())
    private var dataList:[VRTagItemModel] = {
       let arr = ["English","Español","Português","Deutsch","العربية","Italiano","Le français","Filipino"]
        var data = [VRTagItemModel]()
        for str in arr {
            var isSel = false
            if str == "English"{
                isSel = true
            }
            let model = VRTagItemModel(title: str, isSel: isSel)
            data.append(model)
        }
        return data
    }()
    var disCateBlock: ((_ cateType: Int) -> Void)?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        subviews{
            titleLabel
            collectionView
            popularLabel
        }
        
        titleLabel.top(12).leading(15)
        collectionView.leading(0).trailing(0).top(45).height(70)
        popularLabel.leading(15)
        popularLabel.Top == collectionView.Bottom + 20
        
        let layout = UICollectionViewLeftAlignedLayout()
        layout.scrollDirection = .vertical
        layout.minimumLineSpacing = 10
        layout.minimumInteritemSpacing = 10
        layout.sectionInset = UIEdgeInsets(top: 0, left: 10, bottom: 0, right: 10)
        collectionView.style { v in
            v.collectionViewLayout = layout
            v.backgroundColor = .clear
            v.register(VRTagCollectionItem.self, forCellWithReuseIdentifier: VRTagCollectionItem.description())
            v.showsVerticalScrollIndicator = false
            v.delegate = self
            v.dataSource = self
        }
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}


extension VRHomeDiscoverCateView: UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return dataList.count
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        
        guard let item = collectionView.dequeueReusableCell(withReuseIdentifier: VRTagCollectionItem.description(), for: indexPath) as? VRTagCollectionItem else { return UICollectionViewCell() }
        item.model = dataList[indexPath.row]
        return item
    }
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {

        var models = [VRTagItemModel]()
        dataList.forEach { model in
            var itemModel = model
            itemModel.isSel = false
            models.append(itemModel)
        }
        models[indexPath.row].isSel = true
        dataList = models
        collectionView.reloadData()
        
        let cateType = indexPath.row + 5
        disCateBlock?(cateType)
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        let model = dataList[indexPath.row]
//        var w = model.title.caculateWidth(CGSize(width: 85, height: 30), 14.font_medium!)
        var w = (model.title as NSString).size(withAttributes: [NSAttributedString.Key.font: 14.font_medium!]).width
        w = w + 20.0
        return CGSize(width: w, height: 30)
    }
}
